import API from '../../utils/api'
Page({


  data: {
    cityList: [],
    searchResult:[],
    scrollAZ: null,
    scrollNow: 0,
    isScroll: false,
    letterNum:-1,
    nowCity:'成都',
    cityName:''
  },

  onLoad: function (options) {
    this.setData({
      nowCity: wx.getStorageSync('nowCity')
    })
    this.getCity()
  },
  onShow () {
    var cityName = wx.getStorageSync('cityName')
    if (cityName) {
      this.setData({
        cityName: wx.getStorageSync('cityName')
      })
    } 
  },
  // 搜索
  search(e) {
    console.log(e.detail.value)
    var city = this.data.cityList
    var result= []
    city.filter(function (item) {
      if (item.childname.indexOf(e.detail.value)>-1) {
        return result.push(item)
      }
    })
    console.log(result)
    if (e.detail.value == '') {
      result = []
    }
    this.setData({
      searchResult:result
    })
  },
  // 城市选择
  selectCity(e) {
    var dataset = e.currentTarget.dataset
    var cityName = dataset.cityname
    var childid = dataset.childid
    wx.setStorageSync('cityName', cityName)
    wx.setStorageSync('childid', childid)
    // wx.navigateBack({
    //   delta:1
    // })
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  // 获取城市数据
  getCity() {
    API.cityList().then(res => {
      if (res.code == 0) {
        var city = res.Data.allcity
  
        var cityList = []
        for (var k in city) {
          for (var j in city[k].city) {
            cityList.push({ childname: city[k].city[j].childname, childid: city[k].city[j].childid })
          }
        }
        console.log(city)
        this.setData({
          hotcity: res.Data.hotcity,
          allcity: city,
          cityList: cityList
        })
      }
    })
  },
  bindAZ: function (e) {
    console.log(e)
    var currentCityName = e.currentTarget.dataset.id
    var num = e.currentTarget.dataset.index
    console.log(num)
    this.setData({
      letterNum:num
    })
    console.log(currentCityName)
    var that = this;
    //放入A-Z的scrollTop参数
    if (that.data.scrollAZ == null) {
      wx.createSelectorQuery().selectAll('.city-item-A-Z').fields({
        dataset: true,
        size: true,
        rect: true
      }, function (res) {
        console.log(res)
        res.forEach(function (re) {
          console.log(re.dataset.letter)
          if (currentCityName == re.dataset.letter) {
            console.log(re.top)
            console.log(re.top + that.data.scrollNow -100)
            wx.pageScrollTo({
              scrollTop: re.top + that.data.scrollNow -100,
              duration: 0
            })
          }
        })
      }).exec();
    } else {
      this.data.scrollAZ.forEach(function (re) {
        if (currentCityName == re.dataset.letter) {
          wx.pageScrollTo({
            scrollTop: re.top + that.data.scrollNow - 170,
            duration: 500
          })
        }
      })
    }


  },
  onPageScroll: function (e) { // 获取滚动条当前位置
  console.log(e)
  if (e.scrollTop>30) {
    this.setData({
      isScroll : true
    })
  } else {
    this.setData({
      isScroll : false
    })
  }
    this.setData({
      scrollNow: e.scrollTop
    })
  }
})